package com.atsmart.app.webservice;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.MalformedURLException;

import android.util.Log;

import com.atsmart.app.BizApp;
import com.atsmart.app.R;
import com.atsmart.app.exception.BizException;
import com.atsmart.app.http.HttpHelper;
import com.atsmart.app.model.CommonBody;
import com.atsmart.app.utils.PrefsUtils;
import com.google.json.Gson;

public abstract class BaseService {

	protected String getFromService(String urlString)
			throws MalformedURLException, IOException, BizException {
		HttpHelper http = new HttpHelper();
		http.readMode().url(urlString).header("Accept", "application/json")
				.header("Accept-Charset", "utf-8")
				.header("Accept-Encoding", "gzip,deflate")
				.header("Content-type", "application/json");
		try {
			http.send();
		} catch (Exception e) {
			throw new BizException(0);
		}
		String bodyString = "";
		if (http.isSuccessful()) {
			bodyString = http.getBodyAsString();
			if (bodyString != null) {

				return bodyString;
			} else {
				return "";
			}
		} else {
			final int responseCode = http.getResponseCode();
			throw new BizException(responseCode);
		}
	}

 
	protected String postOnService(String urlString, String objectString)
			throws MalformedURLException, IOException, BizException, Exception {
//		Log.e("TAG", "当前URL： = " + PrefsUtils.getURL());
		CommonBody body = new CommonBody();
		body.setCommand(1);
		body.setImsi(BizApp.getInstance().getIMSI());
		body.setVersion(BizApp.getInstance().getString(R.string.app_version));
		String test = new Gson().toJson(body);
//		Log.e("TAG", "url: = " + urlString);
//		Log.e("TAG", "body: = " + test);
//		Log.e("TAG", "object: = " + objectString);
		objectString = test + "\r\n\r\n" + objectString + "\r\n\r\n";
//		Log.e("TAG", "send: = " + objectString);
		final HttpHelper http = new HttpHelper(objectString.length());
		http.createMode().url(urlString).header("Accept", "application/json")
				.header("Accept-Charset", "utf-8")
				.header("Accept-Encoding", "gzip,deflate")
				.header("Content-type", "application/json; charset=utf-8");

		http.body(new ByteArrayInputStream(objectString.getBytes())).send();

		String bodyString = "";

		if (http.isSuccessful()) {
			bodyString = http.getBodyAsString();
//			if(urlString.equals(ServiceHost.getMsgURL())){
//				Log.e("TAG", "返回： = " + bodyString);
//			}
//			 Log.e("筛检前", "bodyString: = "+bodyString + " , url: = " + urlString);
			if (bodyString != null) {
				if (bodyString.split("\r\n\r\n")[0].equals("{\"result\":1}")) {
					PrefsUtils.setResult(bodyString);
					
					if (!urlString.equals(ServiceHost.getLoginURL())
							&& !urlString.equals(ServiceHost.upload())) {
						bodyString = bodyString.substring(
								bodyString.indexOf("\r\n\r\n"),
								bodyString.length());
					}
					
					return bodyString;
				} else {
					PrefsUtils.setResult(null);
					if (!urlString.equals(ServiceHost.getLoginURL())
							&& !urlString.equals(ServiceHost.upload())) {
						bodyString = bodyString.substring(
								bodyString.indexOf("\r\n\r\n"),
								bodyString.length());
					}

					return bodyString;
				}
			} else {
				return "";
			}
		} else {
			final int responseCode = http.getResponseCode();
			throw new BizException(responseCode);
		}

	}
	

	protected String postOnService(String urlString, String str1, String str2)
			throws MalformedURLException, IOException, BizException, Exception {
//		Log.e("TAG", "当前URL： = " + PrefsUtils.getURL());
		String postBody = str1 + "\r\n\r\n" + str2 + "\r\n\r\n";
		HttpHelper http = new HttpHelper(postBody.length());
//		Log.e("send", "send : = " + postBody + "urlString: = " + urlString);
		http.createMode().url(urlString).header("Accept", "application/json")
				.header("Accept-Charset", "GBK")
				.header("Accept-Encoding", "gzip,deflate")
				.header("Content-type", "application/json; charset=utf-8");
		http.body(new ByteArrayInputStream(postBody.getBytes("GBK"))).send();

		String bodyString = "";
		if (http.isSuccessful()) {
			bodyString = http.getBodyAsString();
//			if(urlString.equals(ServiceHost.getMsgURL())){
//				Log.e("TAG", "返回： = " + bodyString);
//			}
//			Log.e("筛检前", "bodyString: = "+bodyString + " , url: = " + urlString);
			
			if (bodyString != null) {
				if (bodyString.split("\r\n\r\n")[0].equals("{\"result\":1}")) {
					PrefsUtils.setResult(bodyString);
					
					if (!urlString.equals(ServiceHost.getLoginURL())
							&& !urlString.equals(ServiceHost.upload())) {
						bodyString = bodyString.substring(
								bodyString.indexOf("\r\n\r\n"),
								bodyString.length());
					}
					
					return bodyString;
				} else {
					PrefsUtils.setResult(null);
					if (urlString!=null&&!urlString.equals(ServiceHost.getMsgURL())) {
						if (urlString.equals(ServiceHost.reportLocationURL())) {
							String[] tmp = bodyString.split("\r\n\r\n");
							if (tmp != null && tmp.length == 2) {
								tmp[0] = tmp[0].substring(0,
										tmp[0].indexOf('}'));
								tmp[1] = tmp[1].substring(
										tmp[1].indexOf('{') + 1,
										tmp[1].length());
								bodyString = tmp[0] + "," + tmp[1];
							}
						} else {
							bodyString = bodyString.substring(
									bodyString.indexOf("\r\n\r\n"),
									bodyString.length());
						}
					} else {
						bodyString = bodyString.substring(
								bodyString.indexOf("\r\n\r\n"),
								bodyString.length());
					}
					return bodyString;
				}
			} else {
				return "";
			}

		} else {
			final int responseCode = http.getResponseCode();
			throw new BizException(responseCode);

		}

	}
}
